package hashset.Demo01;

import java.util.Objects;

public class Student {
    private String name ;
    private int age;

    public Student() {

    }
    // 判定存入对象的唯一性
    // * 两个对象地址值不同,    si !=s2
    // * 两个对象的参数不同,   分数不同    姓名不同
    // *

    @Override
//重写equals方法的目的是判断两个元素的字符是否相同,相同返回true
   public boolean equals(Object o) {
        //判断两个对象的地址值是否相等 相等返回true
        if (this == o) return true;

        //判断插入位置是否为null
        if (o == null || getClass() != o.getClass()) return false;

        //是,则添加对象

        Student student = (Student) o;
        //判断对象age是否相等,不等证明两个对象不等返回false.
        if(age!=student.age)return false;

        boolean b = name != null ? name.equals(student.name) : student.name == null;
        return b;
    }

    @Override
    //重写hashCode方法
    //判断两个hashCode是否相等
    public int hashCode() {
        int result = name!= null?name.hashCode():0;
        result = 31*result+age;
        return  result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

}
